Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Collections.Generic
Imports Microsoft.Practices.EnterpriseLibrary.Data

Namespace DAL.Concesion
	Public Class Contrato
		Public Function Insertar(ByVal _Contrato As BEL.Concesion.Contrato) As System.Boolean
			Dim db As Database = DatabaseFactory.CreateDatabase("ServCons")
			Dim cm As DbCommand = db.GetStoredProcCommand("[SER_I_Contrato]")
			Using cn As DbConnection = db.CreateConnection()
				cn.Open()
				Dim tx As DbTransaction = cn.BeginTransaction()
				Try
					db.AddInParameter(cm, "CO_JURIDICO", DbType.Int32, _Contrato.PROVEEDOR.CO_JURIDICO)
					db.AddInParameter(cm, "CO_SERVICIO", DbType.Int32, _Contrato.SERVICIO.CO_SERVICIO)
					db.AddInParameter(cm, "TX_NUMERO", DbType.String, _Contrato.TX_NUMERO)
					db.AddInParameter(cm, "FE_INICIO", DbType.Date, _Contrato.FE_INICIO)
					db.AddInParameter(cm, "FE_FIN", DbType.Date, _Contrato.FE_FIN)
					db.AddInParameter(cm, "TX_RUTACONTRATO", DbType.String, _Contrato.TX_RUTACONTRATO)
					db.AddInParameter(cm, "SS_MONTO", DbType.Double, _Contrato.SS_MONTO)
					db.AddInParameter(cm, "TX_OBSERVACION", DbType.String, _Contrato.TX_OBSERVACION)
					Using dr As IDataReader = db.ExecuteReader(cm, tx)
						dr.Read()
						_Contrato.CO_CONTRATO = Convert.ToInt16(dr("CO_CONTRATO"))
					End Using

					tx.Commit()
				Catch _ex As Exception
					tx.Rollback()
					Throw New Exception(_ex.Message, _ex.InnerException)
				Finally
					tx.Dispose()
					cm.Dispose()
					tx = Nothing
					cm = Nothing
					db = Nothing
				End Try
			End Using
			Return True
		End Function
		Public Function Modificar(ByVal _Contrato As BEL.Concesion.Contrato) As System.Boolean
			Dim db As Database = DatabaseFactory.CreateDatabase("ServCons")
			Dim cm As DbCommand = db.GetStoredProcCommand("[SER_U_Contrato]")
			Using cn As DbConnection = db.CreateConnection()
				cn.Open()
				Dim tx As DbTransaction = cn.BeginTransaction()
				Try
					db.AddInParameter(cm, "CO_CONTRATO", DbType.Int32, _Contrato.CO_CONTRATO)
					db.AddInParameter(cm, "CO_JURIDICO", DbType.Int32, _Contrato.PROVEEDOR.CO_JURIDICO)
					db.AddInParameter(cm, "CO_SERVICIO", DbType.Int32, _Contrato.SERVICIO.CO_SERVICIO)
					db.AddInParameter(cm, "TX_NUMERO", DbType.String, _Contrato.TX_NUMERO)
					db.AddInParameter(cm, "FE_INICIO", DbType.Date, _Contrato.FE_INICIO)
					db.AddInParameter(cm, "FE_FIN", DbType.Date, _Contrato.FE_FIN)
					db.AddInParameter(cm, "TX_RUTACONTRATO", DbType.String, _Contrato.TX_RUTACONTRATO)
					db.AddInParameter(cm, "SS_MONTO", DbType.Double, _Contrato.SS_MONTO)
					db.AddInParameter(cm, "TX_OBSERVACION", DbType.String, _Contrato.TX_OBSERVACION)
					db.ExecuteNonQuery(cm, tx)

					tx.Commit()
				Catch _ex As Exception
					tx.Rollback()
					Throw New Exception(_ex.Message, _ex.InnerException)
				Finally
					tx.Dispose()
					cm.Dispose()
					tx = Nothing
					cm = Nothing
					db = Nothing
				End Try
			End Using
			Return True
		End Function
		Public Function Listar(ByVal _Parametros As Object()) As DataTable
            Dim ds As New DataSet()
            Dim db As Database = DatabaseFactory.CreateDatabase()
            Dim cm As System.Data.Common.DbCommand = db.GetStoredProcCommand("[SER_S_Contrato_Listado]")
            db.AddInParameter(cm, "TX_NUMERO", DbType.String, Convert.ToString(_Parametros(0)))
            db.AddInParameter(cm, "NO_RUC", DbType.String, Convert.ToString(_Parametros(1)))

            Try
                ds = db.ExecuteDataSet(cm)
                ds.Tables(0).TableName = "CONTRATOS"
                Return ds.Tables(0)
            Catch ex As System.Data.SqlClient.SqlException
                Throw New Exception(ex.Message, ex.InnerException)
            End Try
        End Function
        Public Function Cargar(ByVal intId As Int32) As BEL.Concesion.Contrato
            Dim ds As New DataSet()
            Dim _Contrato As New BEL.Concesion.Contrato
            Dim db As Database = DatabaseFactory.CreateDatabase()
            Dim cm As System.Data.Common.DbCommand = db.GetStoredProcCommand("[SER_S_CONTRATO]")
            db.AddInParameter(cm, "CO_CONTRATO", DbType.Int32, Convert.ToInt32(intId))

            Try
                Using dr As IDataReader = db.ExecuteReader(cm)
                    While dr.Read()
                        _Contrato.EsNuevo = False
                        _Contrato.CO_CONTRATO = Convert.ToInt32(dr("CO_CONTRATO"))
                        _Contrato.PROVEEDOR.CO_JURIDICO = Convert.ToInt32(dr("CO_JURIDICO"))
                        _Contrato.SERVICIO.CO_SERVICIO = Convert.ToInt32(dr("CO_SERVICIO"))
                        _Contrato.TX_NUMERO = Convert.ToString(dr("TX_NUMERO")).Trim()
                        _Contrato.FE_INICIO = Convert.ToDateTime(dr("FE_INICIO"))
                        _Contrato.FE_FIN = Convert.ToDateTime(dr("FE_FIN"))
                        _Contrato.TX_OBSERVACION = Convert.ToString(dr("TX_OBSERVACION")).Trim()
                        _Contrato.TX_RUTACONTRATO = Convert.ToString(dr("TX_RUTACONTRATO")).Trim()
                        _Contrato.SS_MONTO = Convert.ToDouble(dr("SS_MONTO"))
                        _Contrato.NO_ESTADO = Convert.ToString(dr("NO_ESTADO")).Trim()
                    End While
                End Using

                Return _Contrato
            Catch ex As System.Data.SqlClient.SqlException
                Throw New Exception(ex.Message, ex.InnerException)
            End Try
        End Function

        Public Function ModificaEstado(ByVal intId As System.Int32, ByVal strEstado As System.String) As System.Boolean
            Dim db As Database = DatabaseFactory.CreateDatabase()
            Dim cm As DbCommand = db.GetStoredProcCommand("[SER_U_Contrato_Estado]")
            Using cn As DbConnection = db.CreateConnection()
                cn.Open()
                Dim tx As DbTransaction = cn.BeginTransaction()
                Try
                    Dim intRpta As Integer

                    db.AddInParameter(cm, "CO_CONTRATO", DbType.Int16, intId)
                    db.AddInParameter(cm, "NO_ESTADO", DbType.String, strEstado)
                    Using dr As IDataReader = db.ExecuteReader(cm, tx)
                        dr.Read()
                        intRpta = Convert.ToInt32(dr("ROWCOUNT"))
                    End Using
                    tx.Commit()

                    If intRpta > 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Catch _ex As Exception
                    tx.Rollback()
                    Throw New Exception(_ex.Message, _ex.InnerException)
                Finally
                    tx.Dispose()
                    cm.Dispose()
                    tx = Nothing
                    cm = Nothing
                    db = Nothing
                End Try
            End Using
        End Function
	End Class
End Namespace